package com.example.travelhelper.controller;

import com.example.travelhelper.pojo.User;
import com.example.travelhelper.service.serviceImpl.UserServiceImpl;
import com.example.travelhelper.utils.JwtUtil;
import com.example.travelhelper.utils.ResponseData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.security.SecurityProperties;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * @Author: zq
 * @Description: TODO
 * @Date: 2024/4/8 20:38
 * @Version: 1.8
 */
@Controller
@RequestMapping("user")
@ResponseBody
@CrossOrigin
public class UserController {
    /*
    用户登录
     */
    @Autowired
    UserServiceImpl userService;
    @PostMapping("/login")
    public ResponseData userLogin(@RequestParam("uTelephone") String telephone){
        /*
        无论怎样都会登陆成功
         */
        ResponseData responseData = userService.login(telephone);
        User data = (User)responseData.getData();
        /*
        jwt封装
         */
        System.out.println("yes");
        Map<String,String> map = new HashMap<>();
        map.put("uTelephone",data.getUTelephone());
        map.put("uId",data.getUId());
        String token = JwtUtil.getToken(map);
        map.put("token",token);
        //返回 带token
        return ResponseData.success("登录成功", map);
    }

         /*
         显示所有用户
          */
    @GetMapping("/all")
    public ResponseData findAll(){
       return userService.findAll();
    }

}
